/**
 *  HttpClient.h
 *
 *  Copyright (C) 2008  David Andrs <pda@jasnapaka.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#if !defined(AFX_HTTPCLIENT_H__193ACAD1_7C42_47D7_A9DD_8995307FFD48__INCLUDED_)
#define AFX_HTTPCLIENT_H__193ACAD1_7C42_47D7_A9DD_8995307FFD48__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "net/http.h"
#include "net/HttpConnection.h"

#include "PlaylistEntry.h"
#include "../libexpat/expat.h"


// abstract
class CHttpResponseBody {
public:
	virtual void ProcessBuffer(char *buffer, int len) = 0;
	virtual void Finished() = 0;
};


class CNResponse : public CHttpResponseBody {
public:
	CNResponse();

	CString GetResponseValue(const CString &name);

	virtual void ProcessBuffer(char *buffer, int len);
	virtual void Finished();

protected:
	CMapStringToString Data;
};


class CXmlResponse : public CHttpResponseBody {
public:
	CXmlResponse();
	virtual ~CXmlResponse();

	virtual void ProcessBuffer(char *buffer, int len);
	virtual void Finished();

protected:
	XML_Parser Parser;

	virtual void OnCharDataHandler(const XML_Char *s, int len) = 0;
	virtual void OnEndElement(const char *name) = 0;
	virtual void OnStartElement(const char *name, const char **attr) = 0;

	friend void XMLCALL startElement(void *userData, const char *name, const char **attr);
	friend void XMLCALL endElement(void *userData, const char *name);
	friend void XMLCALL charDataHandler(void *userData, const XML_Char *s, int len);
};

class CPlaylistResponse : public CXmlResponse {
public:
	CPlaylistResponse();

	CString Title;
	CList<CPlaylistEntry *, CPlaylistEntry *> Playlist;

protected:
	enum {
		PR_NONE,
		PR_TITLE,
		PR_TRACK_ID,
		PR_TRACK_TITLE,
		PR_TRACK_ALBUM,
		PR_TRACK_LOCATION,
		PR_TRACK_CREATOR,
		PR_TRACK_IMAGE,
		PR_TRACK_DURATION,
	} State;

	CPlaylistEntry *Entry;

	virtual void OnCharDataHandler(const XML_Char *s, int len);
	virtual void OnEndElement(const char *name);
	virtual void OnStartElement(const char *name, const char **attr);
};

// ?? Stream response


//
//
//
class CHttpClient {
public:
	CHttpClient(const CString &hostName, int port = 80);
	virtual ~CHttpClient();

	void Close();
	BOOL SendRequest(const CString &request);
	BOOL ReceiveResponse(CHttpResponseBody *response);

protected:
	CString HostName;
	INTERNET_PORT Port;

	CHttpConnection HttpConnection;
	CHttpRequest *Request;
	CHttpResponse *Response;

/*	CInternetSession Session;
	CHttpConnection *Connection;
	CHttpFile *File;
*/
};

#endif // !defined(AFX_HTTPCLIENT_H__193ACAD1_7C42_47D7_A9DD_8995307FFD48__INCLUDED_)
